<?php
/**
 * Created by PhpStorm.
 * User: allen_ios
 * Date: 17/7/14
 * Time: 下午9:25
 */

class CollegeBaseInfoService extends BaseService
{
    public $accountId;

    public function __construct()
    {
        parent::__construct();
//        $this->accountId = session('account_id');
    }

    /**
     * 通过学校id获取学校信息
     * @param int $schoolId 学校id
     * @return array
     */
    public function getSchoolInfo($schoolId)
    {
        if (!$schoolId || !$this->accountId) {
            return null;
        }
        //先从缓存中去获取
        $cacheKey = "school_{$schoolId}";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }

        //如果没有就从数据库中去取
        $condition = array(
            'account_id' => $this->accountId,
            'school_id' => $schoolId,
            'cut_off' => 0
        );
        $data = D('School')->where($condition)->find();
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取单个学院的信息
     * @param int $collegeId 学院id
     * @return array
     */
    public function getCollegeInfo($collegeId)
    {
        if (!$collegeId || !$this->accountId) {
            return null;
        }

        //先从缓存中去获取
        $cacheKey = "college_{$collegeId}";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }

        //如果没有就从数据库中去取
        $condition = array(
            'account_id' => $this->accountId,
            'college_id' => $collegeId,
            'cut_off' => 0
        );
        $data = D('College')->where($condition)->find();
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取单个专业的信息
     * @param int $majorId 专业id
     * @return mixed
     */
    public function getMajorInfo($majorId)
    {
        if (!$majorId || !$this->accountId) {
            return null;
        }
        //先从缓存中去获取
        $cacheKey = "major_{$majorId}";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }

        //如果没有就从数据库中去取
        $condition = array(
            'account_id' => $this->accountId,
            'major_id' => $majorId,
            'cut_off' => 0
        );
        $data = D('Major')->where($condition)->find();
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取单个班级的信息
     * @param int $classId 班级id
     * @return mixed
     */
    public function getClassInfo($classId)
    {
        if (!$classId || !$this->accountId) {
            return null;
        }
        //先从缓存中去获取
        $cacheKey = "class_{$classId}";
        $cacheData = RedisService::hget($cacheKey);
        if ($cacheData) {
            return $cacheData;
        }

        //如果没有就从数据库中去取
        $condition = array(
            'account_id' => $this->accountId,
            'class_id' => $classId,
            'cut_off' => 0
        );
        $data = D('Class')->where($condition)->find();
        if ($data) {
            RedisService::hset($cacheKey,$data);
            return $data;
        } else {
            return null;
        }
    }

    /**
     * 获取所有学校列表的信息
     * @param
     * @return array
     */
    public function getSchoolList()
    {
        if (!$this->accountId) {
            return null;
        }

        $sids = D('School')->where(array('account_id'=>$this->accountId))->getField('school_id',true);
        if (!$sids) {
            return null;
        }

        $newSchoolList = array();
        foreach ($sids as $sid) {
            $newSchoolList[] = $this->getSchoolInfo($sid);
        }

        return $newSchoolList;
    }

    /**
     * 通过学校id获得学院列表信息
     * @param int $schoolId 学校id
     * @return array|null
     */
    public function getCollegeListBySchoolId($schoolId)
    {
        if (!$schoolId) {
            return null;
        }

        $condition = array(
            'account_id' => $this->accountId,
            'school_id' => $schoolId,
            'cut_off' => 0
        );
        $cids = D('College')->where($condition)->getField('college_id',true);
        if (!$cids) {
            return null;
        }

        $newCollegeList = array();
        foreach ($cids as $cid) {
            $newCollegeList[] = $this->getCollegeInfo($cid);
        }
        return $newCollegeList;
    }

    /**
     * 通过学院id获取当前学院的专业列表信息
     * @param int $collegeId 学院id
     * @return array
     */
    public function getMajorListByCollegeId($collegeId){
        if (!$collegeId) {
            return null;
        }

        $condition = array(
            'account_id' => $this->accountId,
            'college_id' => $collegeId,
            'cut_off' => 0
        );
        $mids = D('Major')->where($condition)->getField('major_id',true);
        if (!$mids) {
            return null;
        }

        $newMajorList = array();
        foreach ($mids as $mid) {
            $newMajorList[] = $this->getMajorInfo($mid);
        }
        return $newMajorList;
    }

    /**
     * 通过专业id获取当前专业的班级列表信息
     * @$param int $majorId 专业id
     * @return array
     */
    public function getClassListByMajorId($majorId){
        if (!$majorId) {
            return null;
        }

        $condition = array(
            'account_id' => $this->accountId,
            'major_id' => $majorId,
            'cut_off' => 0
        );
        $classArr = D('Class')->where($condition)->getField('class_id',true);
        if (!$classArr) {
            return null;
        }
        $newClassList = array();
        foreach ($classArr as $cid) {
            $newClassList[] = $this->getClassInfo($cid);
        }

        return $newClassList;
    }
}